using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication1
{
	/// <summary>
	/// Summary description for Form7.
	/// </summary>
	public class MaterialIssue : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.TextBox txtName;
		private System.Windows.Forms.Button button7;
		private System.Windows.Forms.Panel pnlName;
		private System.Windows.Forms.LinkLabel linkLabel1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtFname;
		private System.Windows.Forms.TextBox txtsid;
		private System.Windows.Forms.Button btnsubmit;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MaterialIssue()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.label1 = new System.Windows.Forms.Label();
            this.txtName = new System.Windows.Forms.TextBox();
            this.button2 = new System.Windows.Forms.Button();
            this.btnsubmit = new System.Windows.Forms.Button();
            this.label11 = new System.Windows.Forms.Label();
            this.txtsid = new System.Windows.Forms.TextBox();
            this.button7 = new System.Windows.Forms.Button();
            this.pnlName = new System.Windows.Forms.Panel();
            this.txtFname = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.linkLabel1 = new System.Windows.Forms.LinkLabel();
            this.pnlName.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(48, 32);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(118, 24);
            this.label1.TabIndex = 0;
            this.label1.Text = "Name of the student";
            // 
            // txtName
            // 
            this.txtName.Location = new System.Drawing.Point(224, 32);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(140, 20);
            this.txtName.TabIndex = 1;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(307, 409);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(121, 28);
            this.button2.TabIndex = 19;
            this.button2.Text = "Cancel";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // btnsubmit
            // 
            this.btnsubmit.Location = new System.Drawing.Point(144, 409);
            this.btnsubmit.Name = "btnsubmit";
            this.btnsubmit.Size = new System.Drawing.Size(128, 28);
            this.btnsubmit.TabIndex = 22;
            this.btnsubmit.Text = "Submit";
            this.btnsubmit.Click += new System.EventHandler(this.btnsubmit_Click);
            // 
            // label11
            // 
            this.label11.Location = new System.Drawing.Point(16, 32);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(60, 24);
            this.label11.TabIndex = 23;
            this.label11.Text = "student id";
            // 
            // txtsid
            // 
            this.txtsid.Location = new System.Drawing.Point(80, 32);
            this.txtsid.Name = "txtsid";
            this.txtsid.Size = new System.Drawing.Size(44, 20);
            this.txtsid.TabIndex = 24;
            // 
            // button7
            // 
            this.button7.Location = new System.Drawing.Point(576, 40);
            this.button7.Name = "button7";
            this.button7.Size = new System.Drawing.Size(116, 24);
            this.button7.TabIndex = 27;
            this.button7.Text = "Check Status";
            this.button7.Click += new System.EventHandler(this.button7_Click);
            // 
            // pnlName
            // 
            this.pnlName.Controls.Add(this.txtFname);
            this.pnlName.Controls.Add(this.label2);
            this.pnlName.Controls.Add(this.label1);
            this.pnlName.Controls.Add(this.txtName);
            this.pnlName.Location = new System.Drawing.Point(144, 8);
            this.pnlName.Name = "pnlName";
            this.pnlName.Size = new System.Drawing.Size(416, 112);
            this.pnlName.TabIndex = 28;
            this.pnlName.Visible = false;
            this.pnlName.Paint += new System.Windows.Forms.PaintEventHandler(this.pnlName_Paint);
            // 
            // txtFname
            // 
            this.txtFname.Location = new System.Drawing.Point(224, 64);
            this.txtFname.Name = "txtFname";
            this.txtFname.Size = new System.Drawing.Size(140, 20);
            this.txtFname.TabIndex = 3;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(48, 64);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(116, 20);
            this.label2.TabIndex = 2;
            this.label2.Text = "Father\'s Name :";
            // 
            // linkLabel1
            // 
            this.linkLabel1.Location = new System.Drawing.Point(40, 64);
            this.linkLabel1.Name = "linkLabel1";
            this.linkLabel1.Size = new System.Drawing.Size(60, 20);
            this.linkLabel1.TabIndex = 29;
            this.linkLabel1.TabStop = true;
            this.linkLabel1.Text = "Forget ID";
            this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
            // 
            // MaterialIssue
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(710, 588);
            this.Controls.Add(this.pnlName);
            this.Controls.Add(this.button7);
            this.Controls.Add(this.txtsid);
            this.Controls.Add(this.label11);
            this.Controls.Add(this.btnsubmit);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.linkLabel1);
            this.Name = "MaterialIssue";
            this.Text = "Course Material Issue";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.Form7_Load);
            this.pnlName.ResumeLayout(false);
            this.pnlName.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion
		SqlConnection con;
		
		private void Form7_Load(object sender, System.EventArgs e)
		{
            con = new SqlConnection();
            con.ConnectionString = "Data Source=ss.cs.luc.edu;Initial Catalog=PrepDB;User ID=ueshwara; pwd=p47807";
            con.Open();		
		}

	
		// Declaring flag - find_stu
		bool find_stu=true;
		CheckBox[] chkbx_course=new CheckBox[10];	
		int i=0;
		int book_count=0;
		string strbook_info="";

		private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
		{
			find_stu=false;
			pnlName.Visible=true;		
		}		

		private void btnsubmit_Click(object sender, System.EventArgs e)
		{
			try
			{
				string strnewissue="";
				for(int j=0;j<book_count;j++)
				{
					if(chkbx_course[j].Checked==true)
					{
						strnewissue+=chkbx_course[j].Text + "$";
					}
				}
				SqlCommand cmd;
				if(strbook_info=="")
				{
					cmd=new SqlCommand("insert into Issuedinformation values('" +txtsid.Text + "','" + strnewissue + "')",con);
				}
				else
				{	
				
					cmd=new SqlCommand("update Issuedinformation set book_received='" + strnewissue + "' where stu_id='" + txtsid.Text + "'",con);
				}
				cmd.ExecuteNonQuery();
				MessageBox.Show("record updated");
				this.Close();
				MaterialIssue mi=new MaterialIssue();
				mi.Show();
			}

			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
			
            
		}
// Event to generate the controls 
		private void button7_Click(object sender, System.EventArgs e)
		{
			foreach(Control ctrl in this.Controls)
			{
				
				if(ctrl!=null)
				{
					if(ctrl.GetType().ToString()=="System.Windows.Forms.CheckBox")
					{
					
						ctrl.Dispose();
						
					}
				}
			}

			con.Close();
			con.Open();
			SqlCommand cmd;
			string cmdstring="";
			if(find_stu==true)
			{
				cmdstring="select course_id from student_details where stu_id='" + txtsid.Text +"'";
			}
			else
			{
				cmdstring="select course_id,Stu_id from student_details where student_name='" + txtName.Text +"' and father_name='" + txtFname.Text + "'";
				
			
			}
			cmd=new SqlCommand(cmdstring,con);

			SqlDataReader drCheck=cmd.ExecuteReader();
			string c_id="";
			if(drCheck.Read())
			{
				c_id=drCheck.GetValue(0).ToString();
				if(find_stu==false)
				{
					
					txtsid.Text=drCheck.GetValue(1).ToString();
				}
			}
			else
			{
				throw new Exception("Information is not correct");
			}
			drCheck.Close();

			cmd=new SqlCommand("select book_id,book_title from bookinformation where course_id='"+ c_id +"'",con);
			SqlDataReader dr=cmd.ExecuteReader();
			int xax=40,yax=170;
			
			while(dr.Read())
			{
				Label lblid=new Label();
				lblid.Size=new Size(100,30);
				lblid.Location=new Point(xax,yax);
				lblid.Text=dr.GetValue(0).ToString();
				this.Controls.Add(lblid);
				/*Label lblnm=new Label();
				lblnm.Size=new Size(200,30);
				lblnm.Location=new Point(xax+110,yax);
				lblnm.Text=dr.GetValue(1).ToString();
				this.Controls.Add( lblnm);*/
				chkbx_course[i]=new CheckBox();
				chkbx_course[i].Size=new Size(400,30);
				chkbx_course[i].Location=new Point(xax+320,yax);
				chkbx_course[i].Text=dr.GetValue(1).ToString();
				//chkbx_course[i].CheckedChanged+=new EventHandler(chkbx_CheckChanged);
				this.Controls.Add( chkbx_course[i]);

				yax=yax+40;
				i=i+1;
				book_count=i;
			}
			

			dr.Close();
			cmd=new SqlCommand("select book_received from Issuedinformation where stu_id='" + txtsid.Text +"'",con);
			SqlDataReader dr1=cmd.ExecuteReader();
			
			

			if(dr1.Read())
			{
				strbook_info=dr1.GetValue(0).ToString();
				string[] str=strbook_info.Split('$');
				for(int j=0;j<str.Length;j++)
				{
					for(int k=0;k<i;k++)
					{
						if(str[j]==chkbx_course[k].Text)
						{
							chkbx_course[k].Checked=true;
							chkbx_course[k].Enabled=false;
						}
					}
				}
			}
			else
			{
				strbook_info="";
			}
					

			dr1.Close();
			i=0;
			/*string str1="";
			str1="select stu_id from student_details where student_name='" + txtName.Text +"' and father_name='" + txtFname.Text + "'";		
			cmd1=new SqlCommand(str1,con);

			//SqlDataReader dr2=cmd1.ExecuteScalar().ToString();
			txtsid.Text=cmd1.ToString();*/

			
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void pnlName_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
		
		}

		
		
		
		

		
	}
}
